package com.example.dell.wisdom_farm;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.example.dell.wisdom_farm.pojo.Sensor;

import java.util.ArrayList;

public class view1 extends Fragment {
    //定义最外层的表格对象
    private GridView gridView;
    //定义一个模拟数据
    private String[] title=new String[]{"空气温度值","土壤湿度值","CO₂浓度值","光照强度值"};
    //声明一个实体集合
   private ArrayList<Sensor> list=new ArrayList<>();
   private MyAdpter myAdpter;


   @Override

   public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup,Bundle bundle){
      View view=inflater.inflate(R.layout.view1,gridView,false);
      gridView=(GridView)view.findViewById(R.id.view2_gridview);
      list.clear();
       list.add(new Sensor("25", "℃", 25.0));
       list.add(new Sensor("50","%",50.0));
       list.add(new Sensor("750","ppm",750.0));
       list.add(new Sensor("15","Klx",15.0));


       myAdpter=new MyAdpter();

       gridView.setAdapter(myAdpter);




       return view;
   }

   public class MyAdpter extends BaseAdapter{


       @Override
       public int getCount() {
           return list.size();
       }

       @Override
       public Object getItem(int position) {
           return null;
       }

       @Override
       public long getItemId(int position) {
           return 0;
       }

       @Override
       public View getView(int position, View convertView, ViewGroup parent) {

           //设置并获取子页面的视图对象
           View view=LayoutInflater.from(getActivity()).inflate(R.layout.view1_item,null);
           //获取子页面中可以改变的标题控件
           TextView textView0=(TextView)view.findViewById(R.id.view2_item0);
           TextView textView1=(TextView)view.findViewById(R.id.view2_item1);
           TextView textView2=(TextView)view.findViewById(R.id.view2_item2);
           TextView textView3=(TextView)view.findViewById(R.id.view2_item3);
           TextView textView4=(TextView)view.findViewById(R.id.view2_item4);
           //GradientDrawable gradientDrawable= (GradientDrawable) textView0.getBackground();
           String sensorName=list.get(position).getSensorNama();
           Double  date=list.get(position).getData();
           String dateUnit=list.get(position).getDataUnit();
           Double  string=date;
           //int x=Integer.parseInt(string);

           //int them=Color.parseColor("#FFFFFF");
           //gradientDrawable.setColor(them);
           if(date>30) {
               textView1.setText("报警");
               textView4.setTextColor(getResources().getColor(R.color.red));
               textView0.setBackgroundResource(R.drawable.view1_cicle_red);
           }else{
               textView1.setText("正常");
               textView4.setTextColor(getResources().getColor(R.color.gray));
               textView0.setBackgroundResource(R.drawable.view1_cicle_white);

           }
            String a;
           a= String.valueOf(date);
           textView3.setText(list.get(position).getDataUnit());
           textView4.setText(title[position]);
           textView2.setText(a);

           return view;
       }
   }

    //


}
